//Page Object
const url = require('../../config/config.js')
Page({
    data: {
        // 用户数据
        resultList: []

    },
    // 设置参数
    pageVo: { size: 5, indexPageNum: 1 },
    // 用户类型 0:客户，1：供应商 2：管理员
    userAndVo: { type: 0 },
    // 总页数
    totalPages: 0,
    //options(Object)
    onLoad: function(options) {
        const { type } = options;
        console.log(type)
        this.userAndVo.type = type;
        // 发起用户请求
        this.getCustomer()
    },
    onShow() {
        //读取token，如果token不存在,直接不给予返回
        const token = wx.getStorageSync("token");
        console.log(token)
        if (!token) {
            wx.navigateTo({
                url: '/pages/login/index'
            });
        }
    },
    // 用户请求
    getCustomer() {
        const header = {
            'content-type': 'application/json',
            'Cookie': "JSESSIONID=" + wx.getStorageSync("token") //读取cookie
        }
        wx.showLoading({
            title: "加载中",
            mask: true,
        });
        wx.request({
            url: url.b.getUserPageList,
            data: { pageVo: this.pageVo, userAndVo: this.userAndVo },
            header: header,
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                try {
                    console.log(result)
                        // 获取总条数
                    const totalCounts = result.data.data.pageResult.totalCount;
                    //计算总页数
                    this.totalPages = Math.ceil(totalCounts / this.pageVo.size);
                    console.log(this.totalPages)
                    const { resultList } = result.data.data;
                    console.log(resultList)
                    this.setData({
                        resultList: [...this.data.resultList, ...resultList]
                    })
                } catch (error) {
                    wx.showToast({
                        title: '登陆过期',
                        icon: 'none',
                    });
                    wx.removeStorageSync({ token: "token" });
                    wx.navigateTo({
                        url: '/pages/login/index',

                    });
                }
            }
        });
        wx.hideLoading();
    },
    // 触底刷新数据
    onReachBottom() {
        console.log('触底了')
            // 如果当前的indexPageNum大于总条数，表示当前没有数据了 
        if (this.pageVo.indexPageNum > this.totalPages) {
            wx.showToast({
                title: '没有数据了',
                icon: 'none',
            });
        } else {
            this.pageVo.indexPageNum++;
            this.getNotask();
        }

    }
});